package com.github.jspxnet.worktask.core.job;

import com.github.jspxnet.json.JSONObject;
import com.github.jspxnet.txweb.table.Member;
import com.github.jspxnet.txweb.util.MemberUtil;
import com.github.jspxnet.utils.BeanUtil;
import com.github.jspxnet.utils.ObjectUtil;
import com.github.jspxnet.worktask.model.enums.FlowNodeEnumType;
import com.github.jspxnet.worktask.model.enums.WorkTaskEnumType;
import com.github.jspxnet.worktask.table.WorkTask;
import com.github.jspxnet.worktask.util.WorkFlowUtil;

import java.util.Date;
import java.util.List;

/**
 * 抄送,生成抄送节点到
 */
public class CopyJobTask extends BaseTaskAction{
    @Override
    public boolean execute() throws Exception {
        //无条件进入下一个节点
        //currentConfFlowNode.getNodeId()
        List<Member>  memberList = userAdapterDAO.getMemberForExpression(confFlowJob.getUsers());
        //先抄送 begin
        for (Member member:memberList)
        {
            WorkTask workTask = BeanUtil.copy(currentWorkTask, WorkTask.class);
            workTask.setUsers(MemberUtil.makeUser(ObjectUtil.toString(member.getId()),member.getName()));
            workTask.setCreateDate(new Date());
            workTask.setWorkTaskType(WorkTaskEnumType.COPYED.getValue());
            workTask.setFlowNodeType(FlowNodeEnumType.END.getValue());
            workTask.setId(0);
            runtimeServiceDAO.save(workTask);
        }

        currentWorkTask.setUsers(workTaskParam.getUsers());
        currentWorkTask.setWorkTaskType(WorkTaskEnumType.COPYED.getValue());
        currentWorkTask.setFlowNodeType(FlowNodeEnumType.END.getValue());
        runtimeServiceDAO.update(currentWorkTask,new String[]{"flowNodeType","workTaskType","users"});
        //先抄送 end
        return true;
    }
}
